<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');
class Course_model extends MY_Model
{
    private $table = 'course';
	public function __construct()
	{
		parent::__construct($this->table);	
	}
	
	
	/**
	 * 获取资讯信息,按分类查找
	 * */
	public function getList($typeId=0,$select='*',$order,$offset=0,$size=20){
        if($typeId){        //分类ID
            $this->db->where('course.typeId',$typeId);            
        }
        $this->db->select($select);
        $this->db->join('expert','expert.expertId=course.expertId');
        $this->db->order_by($order,'desc');
        $res=$this->db->get($this->table,$size,$offset)->result_array();
        return $res;
    }
    /**
     * 获取资讯信息,按分类查找
     * */
    public function getCourse_expert($expertId){
        
        $this->db->select("courseId,title,price,start_time,end_time");        
        $this->db->where('expertId',$expertId);        
        $res=$this->db->get($this->table)->result_array();
        return $res;
    }
    /**
     * 获取资讯主页 资讯列表数据
     *   */
    public function getHome($select='*',$order='createTime',$offset=0,$size=20){
        $this->db->select($select);
        $this->db->where('issue',1); //发布
        $this->db->where('delFlag',0);//未删除状态
        $this->db->join('type','type.typeId=zixun.typeId');
        $res=$this->db->order_by($order,'desc')->get($this->table,$size,$offset)->result_array();
        return $res;
    }
    //获取资讯详情
    public function getDetail($courseId,$select="*"){
        $this->db->select($select);
        $this->db->where('courseId',$courseId);
        $this->db->join('expert','expert.expertId=course.expertId');
        $res=$this->db->get($this->table)->row_array();
        return $res;
    }
    //更新点赞,评论,收藏,浏览量等数据
    public function updateNum($zixunId,$data){
        foreach ($data as $k => $v)
        {
            $this->db->set($k, $v, FALSE);
        }
        $this->db->where('zixunId', $zixunId)->update($this->table);
        return $this->db->affected_rows();
    }
    
    
    //获取数据条数
    public function getCount($search){
        if($search){
            $this->db->like('title',$search);
        }
        return $this->db->get($this->table)->num_rows();
    }
    //后台获取课程数据
    public function getList_admin($page=0,$limit=20,$search=''){
        //$this->db->where();
        if($search){
            $this->db->like('title',$search);
        }
        $this->db->select('course.*,name');
        $this->db->join('expert','expert.expertId=course.expertId','left');
        $res=$this->db->order_by('course.createTime','desc')->get($this->table,$limit,$page*$limit)->result_array();
        return $res;
    }
    //新增数据
    public function add($data){
        $data['createTime']=time();
        $this->db->insert($this->table,$data);
        return $this->db->insert_id();
    }
    //更新数据
    public function update($id,$data){
        $data['updateTime']=time();
        $this->db->where('courseId',$id)->update($this->table,$data);
        return $this->db->affected_rows();
    }
    //删除单个或多个数据
    public function del($courseIds){
        $this->db->where_in('courseId',$courseIds);
        $this->db->delete($this->table);
        return $this->db->affected_rows();
        
    }
    //获取单条信息(后台用);
    public function getCourseRow_admin($courseId){
        $select='course.*,typeName,expert.name as expertName';
        $this->db->select($select);
        $this->db->where('courseId', $courseId);
        $this->db->join('type','type.typeId=course.typeId','left');
        $this->db->join('expert','expert.expertId=course.expertId','left');
        $result = $this->db->get($this->table);
        return $result->row_array();
    }
    
    
    
    
    
    
    
    
    
    
    
    
}
?>